﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Configuration;
using System.Net;
using System.IO;
using System.Text;
using Library.Logging;

namespace SaleManagement.Web.App_Code
{
  public sealed class Utilities
  {
    public static string SafeString(string text)
    {
      if (text == null)
      {
        return null;
      }
      if (text.IndexOf("&nbsp;") == 0)
      {
        text = text.Substring(6);
      }

      text = text.Trim();
      if (text.Length == 0)
      {
        return null;
      }
      return text;
    }

    public static int? ParseInt(string text)
    {
      if (String.IsNullOrEmpty(text))
      {
        return null;
      }

      int number;
      if (int.TryParse(text.Trim(), out number))
      {
        return number;
      }

      return null;
    }

    public static bool SendEmail(string[] to, string content, string header)
    {
      //Gmail SMTP
      MailMessage mailMsg = new MailMessage();
      foreach (string toEmail in to)
      {
        mailMsg.To.Add(toEmail);
      }

      //mailMsg.To.Add("ptcongbk@yahoo.com");
      // From
      MailAddress mailAddress = new MailAddress(ConfigurationManager.AppSettings["SMTPUser"].ToString());
      mailMsg.From = mailAddress;

      // Subject and Body

      mailMsg.Subject = header;
      mailMsg.Body = content;
      mailMsg.IsBodyHtml = true;

      // Init SmtpClient and send
      SmtpClient smtpClient = new SmtpClient(ConfigurationManager.AppSettings["SMTPHost"].ToString());
      bool enableSSL = bool.Parse(ConfigurationManager.AppSettings["SMTPSSL"].ToString());
      smtpClient.EnableSsl = enableSSL;
      int port = int.Parse(ConfigurationManager.AppSettings["SMTPPort"].ToString());
      smtpClient.Port = port;

      smtpClient.UseDefaultCredentials = true;
      smtpClient.Credentials = new NetworkCredential(ConfigurationManager.AppSettings["SMTPUser"].ToString(), ConfigurationManager.AppSettings["SMTPPass"].ToString());

      smtpClient.Send(mailMsg);

      return true;
    }

    public static int GetItemPerPage()
    {
      int temp = 10;
      int.TryParse(ConfigurationManager.AppSettings["itemPerPage"].ToString(), out temp);
      return temp;
    }

    public static string Replace(string sourceString, string searchString, string replaceString, bool caseSensitive)
    {
      if (sourceString == null) throw new ArgumentNullException("sourceString");
      if (searchString == null) throw new ArgumentNullException("searchString");
      if (String.IsNullOrEmpty(searchString)) throw new ArgumentException("searchString cannot be an empty string.", "searchString");
      if (replaceString == null) throw new ArgumentNullException("replaceString");

      StringBuilder retVal = new StringBuilder(sourceString.Length);

      int ptr = 0, lastPtr = 0;

      while (ptr >= 0)
      {
        ptr = sourceString.IndexOf(searchString, ptr, caseSensitive ? StringComparison.InvariantCulture : StringComparison.OrdinalIgnoreCase);

        int strLength = ptr - lastPtr;
        if (strLength > 0 || ptr == 0)
        {
          if (ptr > 0)
            retVal.Append(sourceString.Substring(lastPtr, strLength));

          retVal.Append(replaceString);
          ptr += searchString.Length;
        }
        else
          break;

        lastPtr = ptr;
      }

      if (lastPtr >= 0) //Append the piece of the string left after the last occurrence of searchString, if any.
        retVal.Append(sourceString.Substring(lastPtr));

      return retVal.ToString();
    }

    public static void WriteLog(string description, Exception ex)
    {
      Logger.LogError(description, ex, "Utils Log", null);
    }

  }
}
